package com.alavente.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.alavente.objects.DemandeVente;

public class HibernateDemandeVenteDAO extends HibernateAbstractDAO<DemandeVente> implements GenericDAOInterface<DemandeVente> {

	public DemandeVente getById(int id) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		DemandeVente object = null;
		
		try {
			transaction = session.beginTransaction();			
			object = (DemandeVente) session.get(DemandeVente.class, id);			
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return object;
	}

	public List<DemandeVente> getDemandeVenteClient(int numClient) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeVente> list = new ArrayList<DemandeVente>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeVente demandeVente where demandeVente.client = '" + numClient + "' order by demandeVente.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}

	public List<DemandeVente> getDemandeVenteCategorie(int numCategorie, String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeVente> list = new ArrayList<DemandeVente>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeVente demandeVente where demandeVente.categorie = '" + numCategorie + 
					"' and demandeVente.statut = '" + statut + "' order by demandeVente.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}

	public List<DemandeVente> getDemandeVenteSsCategorie(int numSsCategorie, String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeVente> list = new ArrayList<DemandeVente>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeVente demandeVente where demandeVente.ssCategorie = '" + numSsCategorie +
					"' and demandeVente.statut = '" + statut + "' order by demandeVente.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
	
	public List<DemandeVente> getAllDemandeVente(String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeVente> list = new ArrayList<DemandeVente>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeVente demande where demande.statut = '" + statut + "' order by demande.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
	
	public List<DemandeVente> getDemandesVente(List<Integer> listNumDemande) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeVente> list = new ArrayList<DemandeVente>();
		
		try {
			transaction = session.beginTransaction();
			StringBuilder query = new StringBuilder("from DemandeVente demande where ");
			
			for (Integer numDemande : listNumDemande) {
				query.append("numDemande = " + numDemande + " OR ");
			}
			int length = query.length();
			query.delete(length-3, length-1);
			
			list = session.createQuery(query.toString()).list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
}
